package com.dean.db.service;

import com.dean.batch.entity.BaseEntity;
import com.dean.db.entity.ProcduceParam;
import com.dean.entity.DeanDbConnInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DbServiceImpl<T extends BaseEntity> implements DbService<T>{
    private static final Logger logger = LoggerFactory.getLogger(DbServiceImpl.class);

    @Override
    public int execute(DeanDbConnInfo connInfo, String sql, PreparedStatement statement , String... params) throws SQLException {
        return statement.executeUpdate();
    }

    @Override
    public T queryForObject(DeanDbConnInfo connInfo, String sql, ResultSet resultSet , Class<T> clazz, String... params) throws SQLException, InstantiationException, IllegalAccessException {
        // 获取返回字段信息
        ResultSetMetaData metaData = resultSet.getMetaData();
        // 看看有多少个返回字段
        int columnCount = metaData.getColumnCount();
        // 存入字段
        List<String> fieldNames = new ArrayList<String>(columnCount);
        for(int i = 1;i<=columnCount; i++){
            fieldNames.add(metaData.getColumnLabel(i));
        }
        // 只读取一行数据
        T entity = null;
        if(resultSet.next()){
            entity = clazz.newInstance();
            for (String fieldName : fieldNames) {
                entity.set(fieldName,resultSet.getObject(fieldName));
            }
        }
        return entity;
    }

    @Override
    public List<T> queryForList(DeanDbConnInfo connInfo,String sql,ResultSet resultSet,Class<T> clazz , String... params) throws SQLException, InstantiationException, IllegalAccessException {
        List<T> list = new ArrayList<T>();
        // 获取返回字段信息
        ResultSetMetaData metaData = resultSet.getMetaData();
        // 看看有多少个返回字段
        int columnCount = metaData.getColumnCount();
        // 存入字段
        List<String> fieldNames = new ArrayList<String>(columnCount);
        for(int i = 1;i<=columnCount; i++){
            fieldNames.add(metaData.getColumnLabel(i));
        }
        T entity = null;
        while (resultSet.next()){
            entity = clazz.newInstance();
            for (String fieldName : fieldNames) {
                entity.set(fieldName,resultSet.getObject(fieldName));
            }
            list.add(entity);
        }
        return list.isEmpty()?null:list;
    }

    @Override
    public Map<String, Object> queryForMap(DeanDbConnInfo connInfo,String sql, ResultSet resultSet ,String... params) {
        return null;
    }

    @Override
    public List<Map<String, Object>> queryForMapList(DeanDbConnInfo connInfo,String sql,ResultSet resultSet , String... params) {
        return null;
    }

    @Override
    public Map<String, Object> callProc(DeanDbConnInfo connInfo,String procName,PreparedStatement statement , List<ProcduceParam> procParams) {
        return null;
    }
}
